package com.example.lenovo.findmyclass.Menu;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.example.lenovo.findmyclass.MainActivity;
import com.example.lenovo.findmyclass.R;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;

public class ReviseData extends Activity {
    private Button back;
    private TextView consultation_tv_birthdate,tv_name,tv_nickname,phone_number;
    private Button consultation_bt_birthdate,finish1;
    private int mYear,mMonth,mDay;
    public static String name;
    DateFormat format = DateFormat.getDateInstance();
    Calendar calendar = Calendar.getInstance(Locale.CHINA);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_revise_data);
        final Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);//年
        mMonth = ca.get(Calendar.MONTH);//月
        mDay = ca.get(Calendar.DAY_OF_MONTH);//日
        consultation_tv_birthdate=findViewById(R.id.birthday_tv);
        consultation_bt_birthdate=findViewById(R.id.birthday_bt);
        phone_number=findViewById(R.id.et_phone_number);
        tv_name=findViewById(R.id.tv_name);
        tv_name.setText(name);
        tv_nickname=findViewById(R.id.tv_nickname);
        finish1=findViewById(R.id.finish1);
        finish1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData();
                Intent revise = new Intent(ReviseData.this,PersonalData.class);
                startActivity(revise);
            }
        });
        consultation_bt_birthdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(ReviseData.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                //修改日历控件的年，月，日
                                //这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致
                                calendar.set(Calendar.YEAR,year);
                                calendar.set(Calendar.MONTH,month);
                                calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                                updateTimeShow();
                            }
                        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                datePickerDialog.show();
                updateTimeShow();
            }
        });
        back=findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ReviseData.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
    private void setData(){
        PersonalData.name = tv_name.getText().toString().trim();
        PersonalData.nickname = tv_nickname.getText().toString().trim();
        PersonalData.birthdate=consultation_tv_birthdate.getText().toString().trim();
        PersonalData.phoneNumber=phone_number.getText().toString().trim();
    }
   private void updateTimeShow(){
        //将页面TextView的显示更新为最新时间
        consultation_tv_birthdate.setText(format.format(calendar.getTime()));
    }

}
